package com.tledu.studentmanager.mqtt;

import com.alibaba.fastjson.JSONObject;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;

import java.util.HashMap;

public class MqttProviderCallBack implements MqttCallback {

    private HashMap<String, String> piecesId;
    private HashMap<String, String> operationMap;

    {
        piecesId = new HashMap<>();
        piecesId.put("0", "heiju1");
        piecesId.put("1", "heima1");
        piecesId.put("2", "heixiang1");
        piecesId.put("3", "heishi1");
        piecesId.put("4", "heijiang");
        piecesId.put("5", "heishi2");
        piecesId.put("6", "heixiang2");
        piecesId.put("7", "heima2");
        piecesId.put("8", "heiju2");
        piecesId.put("9", "heipao1");
        piecesId.put("10", "heipao2");
        piecesId.put("11", "heizu1");
        piecesId.put("12", "heizu2");
        piecesId.put("13", "heizu3");
        piecesId.put("14", "heizu4");
        piecesId.put("15", "heizu5");
        piecesId.put("16", "hongzu1");
        piecesId.put("17", "hongzu2");
        piecesId.put("18", "hongzu3");
        piecesId.put("19", "hongzu4");
        piecesId.put("20", "hongzu5");
        piecesId.put("21", "hongpao1");
        piecesId.put("22", "hongpao2");
        piecesId.put("23", "hongju1");
        piecesId.put("24", "hongma1");
        piecesId.put("25", "hongxiang1");
        piecesId.put("26", "hongshi1");
        piecesId.put("27", "hongshuai");
        piecesId.put("28", "hongshi2");
        piecesId.put("29", "hongxiang2");
        piecesId.put("30", "hongma2");
        piecesId.put("31", "hongju2");

        operationMap = new HashMap<>();
        operationMap.put("0", "移动");
        operationMap.put("1", "吃");
        operationMap.put("2", "将军");
        operationMap.put("10", "黑棋获胜");
        operationMap.put("20", "红棋获胜");

    }

    /**
     * 客户端断开连接的回调
     */
    @Override
    public void connectionLost(Throwable throwable) {
        System.out.println("与服务器断开连接，可重连");
    }

    /**
     * 消息到达的回调
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
//        System.out.println(String.format("接收消息主题 : %s",topic));
//        System.out.println(String.format("接收消息Qos : %d",message.getQos()));
//        System.out.println(String.format("接收消息内容 : %s",new String(message.getPayload())));
//        System.out.println(String.format("接收消息retained : %b",message.isRetained()));
        // 棋盘数据
        String jsonStr = new String(message.getPayload());
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        if (jsonObject.containsKey("action")) {
            // 这是一条通知小
//            System.out.println("收到了action消息");
        } else {
            // 这是一条棋谱消息 hongpao 吃 hongshi
            String piece = piecesId.get(jsonObject.getString("pieces"));
            String operation = operationMap.get(jsonObject.getString("operationType"));
            String executedChessPiece = piecesId.get(jsonObject.getString("executedChessPiece"));
            System.out.println(piece + operation + executedChessPiece);
        }
    }

    /**
     * 消息发布成功的回调
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        System.out.println(String.format("接收消息成功"));
    }
}